package com.yc.controller;

import com.yc.bean.OrderBean;
import com.yc.bean.ReviewBean;
import com.yc.model.ResponseResult;
import com.yc.model.ReviewVO;
import com.yc.service.FileService;
import com.yc.service.OrderBiz;
import com.yc.service.ReviewBiz;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * @author zwei
 * @create 2024-10-15 11:27
 */
@RestController
@RequestMapping("cart")
public class ReviewController {
    private FileService fileService;
    private ReviewBiz reviewBiz;
    private OrderBiz orderBiz;
    @Autowired
    public void setFileService(FileService fileService, ReviewBiz reviewBiz, OrderBiz orderBiz) {
        this.fileService = fileService;
        this.reviewBiz = reviewBiz;
        this.orderBiz = orderBiz;
    }
    @PostMapping("addReviews")
    public ResponseResult addReviews(@ModelAttribute ReviewVO reviewVO) throws ExecutionException, InterruptedException {
        String photoFilePath="";        //上传之后图片的地址
        CompletableFuture<String> fileUrlFuture=fileService.upload(reviewVO.getReview_image_url());
        photoFilePath=fileUrlFuture.get();
        ReviewBean reviewBean=new ReviewBean();
        BeanUtils.copyProperties(reviewVO,reviewBean);
        reviewBean.setReview_image_url(photoFilePath);
        reviewBean.setCreatedate(Timestamp.valueOf(LocalDateTime.now()));
        reviewBean.setDisabled(0);
        int i=reviewBiz.addReview(reviewBean);
        if (i>0){
            return ResponseResult.ok();
        }
        return ResponseResult.error();
    }

}
